package com.authine.cloudpivot.ext.applicationservice.api;

import com.alibaba.cola.dto.RequestContext;
import com.authine.cloudpivot.ext.applicationservice.base.ApplicationBaseService;
import com.authine.cloudpivot.ext.constants.ModelConst;
import com.authine.cloudpivot.ext.dto.GatewayParam;
import com.authine.cloudpivot.ext.dto.GatewayResponse;
import com.authine.cloudpivot.ext.dto.api.MemberAddressVo;
import com.authine.cloudpivot.ext.utils.Utils;
import com.authine.mvp.app.launcher.domain.utills.ApplicationContextUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.util.CollectionUtils;

import javax.ws.rs.POST;
import javax.ws.rs.Path;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.authine.hermes.app.launcher.service.ApplicationServiceFoundation.CUSTOM_SERVICE;

/**
 * 用户信息
 */
@Path("MemberV1Service")
@Slf4j
public class MemberV1Service extends ApplicationBaseService {
    private NamedParameterJdbcTemplate jdbcTemplate = ApplicationContextUtils.getBean(NamedParameterJdbcTemplate.class);

    /**
     * 分页查询地址
     *
     * @return
     */
    @Path("addressList")
    @POST
    @Operation(summary = "常用地址-分页查询地址", tags = CUSTOM_SERVICE, description = "分页查询地址")
    public GatewayResponse addressList(@Parameter GatewayParam param) {
        log.info("方法开始:执行类：{},执行方法：addressPages（分页查询地址）,请求人{}" + this.getClass().getName(), RequestContext.getCorpLoginId());
        Object defaultOnlyObj = param.getQuery().get("defaultOnly");
        List<MemberAddressVo> mavoList = new ArrayList<>();
        Map<String, Object> parameter = new HashMap<>();
        StringBuilder sql = new StringBuilder();
        String tableName = getTableName(ModelConst.T_MEMBER_DELIVER_ADDRESS);
        sql.append("select * from " + tableName + " where deleted ='0' and member_id like '%" + RequestContext.getCorpLoginId() + "%'");
        if (null != defaultOnlyObj && Utils.parseObjToBool(defaultOnlyObj)) {
            sql.append(" and default_address =:default_address ");
            parameter.put("default_address", "0");
        }
        log.info("方法开始:执行类：{},执行方法：addressPages（分页查询地址）,请求人{}", sql);
        List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql.toString(), parameter);
        if (!CollectionUtils.isEmpty(resultList)) {
            for (Map<String, Object> mdae : resultList) {
                boolean defaultAddress = null != mdae.get("default_address") && mdae.get("default_address").equals("0") ? false : true;
                MemberAddressVo memberAddressVo = new MemberAddressVo((String) mdae.get("id"), (String) mdae.get("area"),
                        (String) mdae.get("address"), (String) mdae.get("post"), (String) mdae.get("alias"), defaultAddress,
                        (String) mdae.get("receiver_name"), (String) mdae.get("mobile"), (String) mdae.get("phone"),
                        (String) mdae.get("skype"), (String) mdae.get("email"), (String) mdae.get("qq"));
                mavoList.add(memberAddressVo);
            }
        }
        return GatewayResponse.success(mavoList.size(),"查询成功", mavoList);
    }
}
